package cn.tianjindong.blog_manager.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleCategory;
import cn.tianjindong.blog.pojo.BlogManager;
import cn.tianjindong.blog_manager.service.ArticleService;
import cn.tianjindong.blog_manager.service.CategoryService;

/**
 * 文章相关功能Controller
 * @author 田 金 东
 *
 */
@Controller
public class ArticleController {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private CategoryService categoryService;

	/**
	 * 跳转到“文章添加”页面
	 * 
	 * @return
	 */
	@RequestMapping("/add-article.html")
	public String toAddArticlePage(Model model) {
		List<BlogArticleCategory> categoryList = categoryService.getAllCategory();
		model.addAttribute("categoryList", categoryList);
		return "/add-article";
	}

	/**
	 * 跳转到“文章”页面
	 * 
	 * @return
	 */
	@RequestMapping("/article.html")
	public String toArticlePage(Model model, @RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "8") Integer countItem) {
		PageView<BlogArticle> pageView = articleService.getArticlePageView(currentPage, countItem);
		model.addAttribute("pageView", pageView);
		return "/article";
	}

	/**
	 * 添加文章
	 * 
	 * @param article
	 * @param keywords
	 * @return
	 */
	@RequestMapping("/article/add.action")
	public String addArticle(HttpSession session, BlogArticle article, String keywords) {
		BlogManager user = (BlogManager) session.getAttribute("user");
		if (user != null) {
			articleService.addArticle(user, article, keywords);
			return "redirect:/article.html";
		} else {
			return "redirect:/login.html";
		}
	}

	/**
	 * 前往文章修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/article/update/{id}.html")
	public String toUpdateArticlePage(Model model, @PathVariable Long id) {
		List<BlogArticleCategory> categoryList = categoryService.getAllCategory();
		model.addAttribute("categoryList", categoryList);
		BlogArticle article = articleService.getArticleById(id);
		model.addAttribute("article", article);
		return "update-article";
	}

	/**
	 * 修改文章
	 * 
	 * @param session
	 * @param article
	 * @param keywords
	 * @return
	 */
	@RequestMapping("/article/update.action")
	public String updateArticle(HttpSession session, BlogArticle article, String keywords) {
		BlogManager user = (BlogManager) session.getAttribute("user");
		if (user != null) {
			articleService.updateArticle(user, article, keywords);
			return "redirect:/article.html";
		} else {
			return "redirect:/login.html";
		}
	}

	/**
	 * 
	 * @param id 文章的ID
	 * @return
	 */
	@RequestMapping("/article/delete/{id}.action")
	public String deleteArticle(@PathVariable Long id) {
		articleService.deleteArticle(id);
		return "redirect:/article.html";
	}
}
